package com.august.controller;

import com.august.bean.Employees;
import com.august.bean.Msg;
import com.august.service.EmployeesService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author : Crazy_August
 * @description : 处理员工CRUD请求
 * @Time: 2022-02-12   19:46
 */
@Controller
public class EmployeesController {

    @Autowired
    EmployeesService employeeService;

    
    @RequestMapping(value = {"/emps", "/"}, method = {RequestMethod.GET})
    @ResponseBody
    public Msg getAllEmployeesWithDepartment(@RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        //获取第1页，10条内容，默认查询总数count
        PageHelper.startPage(page, pageSize);
        List<Employees> emps = employeeService.getAllEmployeesWithDepartment();
        //用PageInfo对结果进行包装
        PageInfo<Employees> PageInfo = new PageInfo(emps, pageSize);
        System.out.println(PageInfo);
        return Msg.success().add("pageInfo", PageInfo);
    }

    @RequestMapping(value = "/emps", method = {RequestMethod.POST})
    @ResponseBody
    public Msg addEmployees(@RequestBody Employees employees) {
        System.out.println(employees);
        employeeService.addEmployees(employees);
        return Msg.success();
    }

    @RequestMapping(value = "/emps", method = {RequestMethod.PUT})
    @ResponseBody
    public Msg updateEmployees(@RequestBody Employees employees) {
        employeeService.updateEmployees(employees);
        return Msg.success();
    }

    @RequestMapping(value = "/emps/{empId}", method = {RequestMethod.DELETE})
    @ResponseBody
    public Msg deleteEmployees(@PathVariable("empId") Integer empId) {
        System.out.println(empId);
        employeeService.deleteEmployees(empId);
        return Msg.success();
    }


    /**
     * 批量删除
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/emps", method = {RequestMethod.DELETE})
    @ResponseBody
    public Msg deleteBatchEmployees(@RequestBody Map<String, Object> empIds) {
        List<Integer> Ids = (List<Integer>) empIds.get("empIds");
        System.out.println(Ids);
        employeeService.deleteBatchEmployees(Ids);
        return Msg.success();
    }


    @RequestMapping(value = "/emp", method = {RequestMethod.GET})
    @ResponseBody
    public Msg getEmployee(@RequestParam("empId") Integer empId) {
        Employees employee = employeeService.getEmployee(empId);
        return Msg.success().add("emp", employee);
    }


}
